package com.zhy.day01.getdata;

import com.zhy.day01.dao.MovieDao;
import com.zhy.day01.utils.DBUtil;
import com.zhy.day01.domain.Movie;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;



/**
 * @author Lenovo
 * @version 1.0
 * @description TODO
 * @date 31/7/2023 下午2:07
 */

public class GetMovieData {
    // 参数1
    static List<Movie> movies = new ArrayList<Movie>();
    static Integer rank = 1;

    public static List<Movie> getData(String url, HttpClient httpClient) throws IOException {

        String UserAgent ="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54";
        String Host="movie.douban.com";
        String Accept="text/html";
        String AcceptCharset="utf-8";
        String AcceptEncoding="gzip";
        String AcceptLanguage="en-US,en";
        //1.创建get请求
        HttpGet get  = new HttpGet(url);
        get.setHeader("User-Agent",UserAgent);
        get.setHeader("Host",Host);
        get.setHeader("Accept",Accept);
        get.setHeader("AcceptCharset",AcceptCharset);
        // 2.创建接受的响应对象

        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,"ok");
        response = httpClient.execute(get);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 200){
            String s = EntityUtils.toString(response.getEntity(), "utf-8");
            Document parse = Jsoup.parse(s);
            Elements elements = parse.select("div[class=item]");
            for (Element element : elements) {
                String name = element.select("div[class=hd]").select("a").select("span[class=title]").text();
                String desc = element.select("div[class=bd]").select("p").eq(0).text();
                String score = element.select("div[class=star]").select("span[class=rating_num]").text();
                String numb = element.select("div[class=star]").select("span").eq(3).text();
                String num = numb.substring(0, numb.indexOf("人"));
                Movie movie  = new Movie(rank++,name,desc,Double.valueOf(score),Integer.valueOf(num));
                movies.add(movie);
            }
            String hurl = parse.select("div[class=paginator]").select("span[class=next]").select("a").attr("href");
            if (hurl == null || "".equals(hurl)){

            }else {
                String oldUrl="https://movie.douban.com/top250";
                hurl = oldUrl +hurl;
                getData(hurl,httpClient);
            }



        }
        return movies;
    }


    public static void main(String[] args) throws IOException {


        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient client = httpClientBuilder.build();
        List<Movie> data = GetMovieData.getData("https://movie.douban.com/top250", client);
        SqlSession ss = DBUtil.getSqlSession();
        MovieDao mapper = ss.getMapper(MovieDao.class);
        for (Movie datum : data) {

            mapper.addMovie(datum);
            System.out.println(datum);
            //datum.toString();
        }
    }

}
